package no.doomsday.sync 
{
	import flash.display.Shape;
	import flash.events.Event;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	/**
	 * A simple utility for getting a synchronised beat out of a playing sound
	 * @author Andreas Rønning
	 */
	public class DoomsdaySync 
	{
		private var _updateSource:Shape = new Shape();
		private var _bpm:String;
		private var _channel:SoundChannel;
		private var _syncPattern:ISyncPattern;
		
		public function DoomsdaySync() 
		{
			
		}
		public function start(sc:Sound, syncPattern:ISyncPattern) {
			_syncPattern = syncPattern;
			_bpm = syncPattern.getBPM(0);
			_channel = sc.play(0);
			_updateSource.addEventListener(Event.ENTER_FRAME, update);
		}
		public function stop():void {
			_channel.stop();
			_updateSource.removeEventListener(Event.ENTER_FRAME, update);
		}
		private function update(e:Event = null):void {
			var pos:Number = _channel.position;
			if (!_syncPattern.getIsSilent(pos)) {
				_syncPattern.getBPM(pos);
			}
		}
		
	}

}